/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.data;

import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.gadglet.admin.gadget.shared.GadgetTransit;
import com.gadglet.data.utils.PMF;
import com.gadglet.params.SharedConstants;
import com.google.appengine.api.datastore.Text;

/**
 * Persistence class for Gadget definitions
 *
 */
@PersistenceCapable
public class Gadget {
	// system level parameters
	@PrimaryKey
	@Persistent
	private String name;
	
	@Persistent
	private String title;

	// is this published yet or no ...
	@Persistent
	private int status;

	// this parameter will store gadgets type: iGoogle, GmailSideBar,
	// CalanderEvent, ext
	@Persistent
	private int type;

	@Persistent
	private String gadgletType; // gadget or minglet

	@Persistent
	private String handlerClassName; // RequestHandler packgename

	@Persistent
	private Date creation;

	@Persistent
	private String helpUrl;

	@Persistent
	private String gadgetFileName;

	@Persistent
	private Text description;

	// content of gadget
	@Persistent
	private Text xmlSource;

	public Gadget(GadgetTransit gadget) {
		if (gadget != null) {
			setName(gadget.getName());
			setTitle(gadget.getTitle());
			setDescription(gadget.getDescription());
			setHelpUrl(gadget.getHelpUrl());
			setXmlSource(gadget.getXmlSource());
			setGadgetFileName(gadget.getGadgetFileName());
			setType(gadget.getType());
			setStatus(gadget.getStatus());
			setCreation(new java.util.Date());
			setGadgletType(gadget.getGadgletType());
			setHandlerClassName(gadget.getHandlerClassName());
		}

	}

	public Gadget() {
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setGadgetFileName(String gadgetFileName) {
		this.gadgetFileName = gadgetFileName;
	}

	public String getGadgetFileName() {
		return gadgetFileName;
	}

	public String getXmlSource() {
		if (xmlSource == null)
			return null;

		return xmlSource.getValue();
	}

	public String getDescription() {

		if (this.description == null)
			return null;

		return description.getValue();
	}

	public void setDescription(String description) {
		this.description = new Text(description);
	}

	public void setXmlSource(String xmlSource) {
		this.xmlSource = new Text(xmlSource);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Date getCreation() {
		return creation;
	}

	public void setCreation(Date creation) {
		this.creation = creation;
	}

	public String getHelpUrl() {
		return helpUrl;
	}

	public void setHelpUrl(String helpUrl) {
		this.helpUrl = helpUrl;
	}

	public void setGadgletType(String type) {
		this.gadgletType = type;
	}

	public String getGadgletType() {
		return this.gadgletType;
	}

	public void setHandlerClassName(String handlerClassName) {
		this.handlerClassName = handlerClassName;
	}

	public String getHandlerPackageName() {
		return this.handlerClassName;
	}

	public boolean add() {
		// check security
		if (!DomainUserUtils.isOpenIdUserAdmin() || this.name == null)
			return false;
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			pm.makePersistent(this);
		} catch (Exception e) { // log
			return false;
		} finally {
			pm.close();
		}
		return true;
	}

	public boolean delete() {
		// check security
		if (!DomainUserUtils.isOpenIdUserAdmin())
			return false;
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			pm.deletePersistent(this);
		} catch (Exception e) {
			// TBD, add log
			return false;
		} finally {
			pm.close();
		}
		return true;
	}

	public boolean isVisible() {
		// TODO need to see in case server is for one domain only, then show
		// only to login users.
		if (DomainUserUtils.isOpenIdUserAdmin()
				|| getStatus() == SharedConstants.gadgetStatusOnLine)
			return true;
		else
			return false;
	}

	public GadgetTransit toRpcGadegt() {
		com.gadglet.admin.gadget.shared.GadgetTransit gadget = new GadgetTransit();
		gadget.setName(getName());
		gadget.setTitle(getTitle());
		gadget.setHelpUrl(getHelpUrl());
		gadget.setDescription(getDescription());
		gadget.setXmlSource(getXmlSource());
		gadget.setGadgetFileName(getGadgetFileName());
		gadget.setType(getType());
		gadget.setStatus(getStatus());
		gadget.setGadgletType(getGadgletType());
		gadget.setHandlerClassName(getHandlerPackageName());
		return gadget;
	}
}
